package com.framework.service.impl;


import com.baomidou.mybatisplus.mapper.EntityWrapper;
import com.baomidou.mybatisplus.service.impl.ServiceImpl;
import com.framework.dao.TranTerBenefitDayDao;
import com.framework.entity.sys.SysAgent;
import com.framework.entity.tran.TranTerBenefitDay;
import com.framework.entity.tran.TranTransactionInfo;
import com.framework.service.TranTerBenefitDayService;
import com.framework.service.TranTerBenefitInfoService;
import com.framework.utils.DateUtils;
import com.framework.utils.string.StringUtil;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.math.BigDecimal;
import java.util.Date;
import java.util.List;

@Service("tranTerBenefitDayService")
public class TranTerBenefitDayServiceImpl extends ServiceImpl<TranTerBenefitDayDao,TranTerBenefitDay> implements TranTerBenefitDayService  {

    @Autowired
    private TranTerBenefitInfoService tranTerBenefitInfoService;

    @Override
    public void insertTranTerBenefitDay(TranTerBenefitDay benefitDay) {
        baseMapper.insertTranTerBenefitDay(benefitDay);
    }

    @Override
    public List<TranTerBenefitDay> queryReback(Long deptId, Date date) {
        return baseMapper.selectList(new EntityWrapper<TranTerBenefitDay>().eq("dept_id",deptId).eq("benefit_date",date));
    }

    @Override
    public void addNewRecord(SysAgent agent, TranTransactionInfo info, BigDecimal topOwnBenefit) {
        TranTerBenefitDay t=new TranTerBenefitDay();
        t.setBenefitDate(DateUtils.format(info.getTransactionDate(),DateUtils.DATE_PATTERN));
        t.setOwnerId(agent.getAgentId());
        t.setSn(info.getSn());
        TranTerBenefitDay day = baseMapper.selectOne(t);
        String terBenefitId=verifyBenefitId(StringUtil.getDateString(4));
        if(day==null){
            TranTerBenefitDay terBenefitDay=new TranTerBenefitDay();
            terBenefitDay.setBenefitDate(DateUtils.format(info.getTransactionDate(),DateUtils.DATE_PATTERN));
            terBenefitDay.setCreateDate(new Date());
            terBenefitDay.setAllTranAmount(info.getAmount());
            terBenefitDay.setSn(info.getSn());
            terBenefitDay.setAllBenefitAmount(topOwnBenefit);
            terBenefitDay.setOwnerId(agent.getAgentId());
            terBenefitDay.setDeptId(info.getDeptId());
            terBenefitDay.setTerBenefitId(terBenefitId);
            baseMapper.insert(terBenefitDay);
        }else {
            BigDecimal tranAmount=day.getAllTranAmount();
            BigDecimal benefitAmount=day.getAllBenefitAmount();
            tranAmount=tranAmount.add(info.getAmount());
            benefitAmount=benefitAmount.add(topOwnBenefit);
            t.setAllTranAmount(tranAmount);
            t.setAllBenefitAmount(benefitAmount);
            baseMapper.updateById(t);
        }
        tranTerBenefitInfoService.addNewRecord(terBenefitId,info,topOwnBenefit);

    }
    @Override
    public String verifyBenefitId(String terBenefitId) {
        TranTerBenefitDay t = baseMapper.selectById(terBenefitId);
        if(t!=null){
            terBenefitId=StringUtil.getDateString(4);
            verifyBenefitId(terBenefitId);
        }
        return terBenefitId;
    }

}
